package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.aop.annotation.DataScope;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.SysOrderEntity;
import com.company.project.service.SysContentService;
import com.company.project.service.SysOrderService;
import com.company.project.util.ExcelUtil;
//import com.company.project.util.ExcelUtils;
import com.company.project.util.TransactionHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 订单管理
 *
 * @author Jake
 * @version V1.0
 */
@Slf4j
@Api(tags = "订单管理")
@RestController
@RequestMapping("/sysOrder")
public class SysOrderController {
    @Resource
    private SysOrderService sysOrderService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @RequiresPermissions("sysOrder:add")
    public DataResult add(@RequestBody SysOrderEntity sysOrder) {
        sysOrderService.save(sysOrder);
        return DataResult.success();
    }


    @ApiOperation(value = "上传excel")
    @PostMapping("/import")
    @RequiresPermissions(value = {"sysOrder:add", "sysContent:add"}, logical = Logical.OR)
//    @RequiresPermissions("sysOrder:import")
    public DataResult importFile(@RequestPart(value = "file") MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            Map<String, String> map = new HashMap<>();
            map.put("采购单号", "purchaseId");
            map.put("商品编号", "orderId");
            map.put("商品名称", "eName");
            map.put("销售价格", "ePrice");
            map.put("销售数量", "eNumber");
            map.put("销售总金额", "eTotalPrice");
            map.put("采购员", "jdPerson");
            map.put("订购时间", "createTime");

            List<SysOrderEntity> list = ExcelUtil.excelToList(fileName , inputStream, SysOrderEntity.class, map, "yyyy-MM-dd HH:mm:ss");

            for (SysOrderEntity entity : list) {
                sysOrderService.save(entity);
            }
//            sysOrderService.saveBatch(list);

            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("上传失败...");
        }
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @RequiresPermissions("sysOrder:delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysOrderService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @RequiresPermissions("sysOrder:update")
    public DataResult update(@RequestBody SysOrderEntity sysOrder) {
        sysOrderService.updateById(sysOrder);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @RequestMapping("/listByPage")
    @RequiresPermissions("sysOrder:list")
    @ResponseBody
    @DataScope
    public DataResult findListByPage(@RequestBody SysOrderEntity sysOrder) {
        System.out.println("[findListByPage] , findListByPage.....sysOrder"+sysOrder);
        Page page = new Page(sysOrder.getPage(), sysOrder.getLimit());
        LambdaQueryWrapper<SysOrderEntity> queryWrapper = Wrappers.lambdaQuery();
//        //查询条件示例
//        if (!StringUtils.isEmpty(sysOrder.getE_name())) {
//            queryWrapper.like(SysOrderEntity::getE_name, sysOrder.getE_name());
//        }
//
        IPage<SysOrderEntity> iPage = sysOrderService.page(page, queryWrapper);
        List<SysOrderEntity> list = iPage.getRecords();
        for (SysOrderEntity entity : list) {
            System.out.println("entity :::" + entity);
        }
        return DataResult.success(iPage);
    }
}
